import { _decorator, AudioClip, BoxCollider2D, Collider2D, Component, Contact2DType, find, instantiate, IPhysics2DContact, Node, Prefab, RigidBody2D, UITransform, Vec2 } from 'cc';
import { Ball } from './Ball';
import { Prop } from './Prop';
import { Canvas } from './Canvas';
import { Permanent } from './Permanent';
const { ccclass, property } = _decorator;

@ccclass('Slider')
export class Slider extends Component {


    @property(Prefab)
    private ball:Prefab = null

    spead:number = 5

    private rigidBody:RigidBody2D = null
    private uiTransform:UITransform = null
    private collider:BoxCollider2D = null
    private lv

    private move:number = 0

    ballMultiplyThree:number = 0
    threeBall:number = 0
    width:number = 200
    widthnum:number = 0

    start() {
        this.spead = find('Permanent').getComponent(Permanent).silderspead
        this.collider = this.node.getComponent(BoxCollider2D)
        this.rigidBody = this.node.getComponent(RigidBody2D)
        this.uiTransform =  this.node.getComponent(UITransform)
        this.collider.on(Contact2DType.BEGIN_CONTACT,this.onBeginContact,this)
    }

    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null){
        if(find('Canvas').getComponent(Canvas).gameover == 1){
            return
        }
        if(this.node.children.length > 30 ){
            if(otherCollider.node.getComponent(Prop)){
                otherCollider.node.getComponent(Prop).die()
            }
            return
        }
        if(otherCollider.node.name === 'prop_1'){
            this.ballMultiplyThree = 2
            otherCollider.node.getComponent(Prop).die()
        }
        if(otherCollider.node.name === 'prop_2'){
            this.threeBall = 2
            otherCollider.node.getComponent(Prop).die()
        }
        if(otherCollider.node.name === 'prop_3'){
            this.widthnum = 600
            otherCollider.node.getComponent(Prop).die()
        }
    }

    // 生成三个球
    transmit(){
        let ballPre = instantiate(this.ball)
        this.node.addChild(ballPre)
        ballPre.setPosition(0,40,0)
        ballPre.getComponent(RigidBody2D).linearVelocity = new Vec2(0,30)
        ballPre = instantiate(this.ball)
        this.node.addChild(ballPre)
        ballPre.setPosition(0,40,0)
        ballPre.getComponent(RigidBody2D).linearVelocity = new Vec2(30*Math.sin(10*Math.PI/180),30*Math.cos(10*Math.PI/180))
        ballPre = instantiate(this.ball)
        this.node.addChild(ballPre)
        ballPre.setPosition(0,40,0)
        ballPre.getComponent(RigidBody2D).linearVelocity = new Vec2(30*Math.sin(350*Math.PI/180),30*Math.cos(350*Math.PI/180))
    }

    // 道具3，挡板放大效果
    amplify(w:number){
        if(this.width == w){
            return
        }else{
            this.width = w
            this.uiTransform.width = w
            this.collider.size.width = w
            this.collider.apply()
        }
        
    }



    Move(m){
        this.move = m
    }

    update(deltaTime: number) {
        this.lv = this.node.getComponent(RigidBody2D).linearVelocity
        if(this.move>0){
            this.lv.x = this.spead*Math.sqrt(this.move*this.move)
        }else{
            this.lv.x = this.spead*Math.sqrt(this.move*this.move)*-1
        }
        
        this.node.getComponent(RigidBody2D).linearVelocity = this.lv
        this.move = 0

        if(this.node.children.length == 0){
            console.log('Gamen Over')
        }

        if(this.ballMultiplyThree > 0 ){
            this.ballMultiplyThree --
        }

        if(this.threeBall == 1){
            this.transmit()
        }

        if(this.threeBall > 0 ){
            this.threeBall --
        }

        if(this.widthnum > 0){
            this.amplify(400)
            this.widthnum--
        }else{
            this.amplify(200)
        }
    }
}


